from unittest import TestCase
import requests
from ddt import ddt, data, unpack
from DataTest import DataTest
from BuyOperation import BuyOperation


@ddt
class TestBuy(TestCase):
    data_test = DataTest()

    @data(*data_test.data_test)
    @unpack
    def testBuy(self, usr, pwd, product_id, user_id, paypwd):
        print('hello, world!')
        self.session = requests.session()
        self.url = 'http://localhost:90'
        login_path = '/login/verifyLogin'
        param = {
            "username": usr,
            "password": pwd
        }
        response = self.session.get(url=self.url + login_path, params=param)

        response.encoding = "utf-8"

        print("登陆状态码为：", response.status_code)
        print(response.text)
        buy_path = "/user/userBuyChangeMoney"

        buy_param = {
            "changeMoneyId": product_id,
            "userId": user_id,
            "passWord": paypwd
        }

        buyoperation = BuyOperation()
        buy_resp = buyoperation.buy_operation(self.session, self.url + buy_path, buy_param)

        print(buy_resp.status_code)
        print(buy_resp.text)
        self.assertEqual(buy_resp.status_code, 200)

